package org.springcloud.wisdom.spring_02_classroom.student.attendence.dao;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.spring_02_classroom.student.attendence.entity.AttendenceEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AttendenceDao {
    @Insert("INSERT INTO attendence(cid, count, chour, sid, signcount) VALUES(#{cid}, 1, #{chour}, #{sid}, 0)")
    int insertAttendence(AttendenceEntity attendenceEntity);

    // 更新考勤记录的count
    @Update("UPDATE attendence SET count = count + 1 WHERE cid = #{cid} AND sid = #{sid}")
    int updateAttendence(AttendenceEntity attendenceEntity);

    // 更新考勤记录的signcount
    @Update("UPDATE attendence SET signcount = signcount + 1 WHERE cid = #{cid} AND sid = #{sid}")
    int updateSigncountByCidAndSid(@Param("cid") String cid, @Param("sid") String sid);

    @Select("SELECT * FROM attendence WHERE cid = #{cid} AND sid = #{sid}")
    AttendenceEntity selectByCidAndSid(@Param("cid") String cid, @Param("sid") String sid);




    // 学生根据sid和cid查看考勤表
    @Select("SELECT a.*, c.cname, t.tname " +
            "FROM attendence a " +
            "INNER JOIN course c ON a.cid = c.cid " +
            "INNER JOIN teacher t ON c.tid = t.tid " +
            "WHERE a.sid = #{sid} AND c.isdel=0")
    List<AttendenceEntity> attendenceBySidAndCid(@Param("sid") String sid);

    //老师查看考勤表
    @Select("<script>" +
            "SELECT a.*, s.sname " +
            "FROM attendence a " +
            "INNER JOIN student s ON a.sid = s.sid " +
            "<where> " +
            "  <if test='cid != null'> " +
            "    a.cid = #{cid} " +
            "  </if> " +
            "  <if test='sname != null'> " +
            "    AND s.sname = #{sname} " +
            "  </if> " +
            "</where> " +
            "LIMIT #{start}, #{size}" +
            "</script>")
    public List<AttendenceEntity> get(@Param("cid") String cid,@Param("sname") String sname,
                                      @Param("start") int start, @Param("size") int size);

    @Select("<script>" +
            "SELECT count(*) FROM attendence a " +
            "INNER JOIN student s ON a.sid = s.sid " +
            "<where> " +
            "  <if test='cid != null'> " +
            "    a.cid = #{cid} " +
            "  </if> " +
            "  <if test='sname != null'> " +
            "   AND s.sname = #{sname} " +
            "  </if> " +
            "</where> " +
            "</script>")
    public int Count(String cid,String sname);
}
